package band.wukong.practice.javase.core.io.readerandwriter;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 * Desc:
 * Author: sunjs
 * Date: 2009-11-16 16:15:49
 */
public class TestStreamRW {
    static final String FILE_IN = "E:\\workspace\\TestJava\\src\\com\\jsun\\javase\\io\\doc\\poem.txt";
    static final String FILE_OUT = "E:\\workspace\\TestJava\\src\\com\\jsun\\javase\\io\\doc\\out.txt";

    public static void main(String[] args) throws Exception {
        testWriter();
    }

    public static void testReader() throws Exception {
        FileInputStream is = new FileInputStream(FILE_IN);
        InputStreamReader reader = new InputStreamReader(is, "UTF-8");
//        InputStreamReader reader = new InputStreamReader(is);

        int data;
        while ((data = reader.read()) != -1) {
            System.out.print((char) data);
        }
        reader.close(); //close方法不执行任何操作

        //为了提高效率，可以用BufferedReader来装饰InputStreamReader
    }

    public static void testWriter() throws Exception {
        FileOutputStream os = new FileOutputStream(FILE_OUT);
        OutputStreamWriter writer = new OutputStreamWriter(os, "UTF-8");
        writer.write("精诚所至，金石为开");
        writer.close();
    }
}
